package com.zizy.healthexpert.Database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

//T_NUTRITION
public class MyDataBase_T_NUTRITION {
	static final int PLACETOTAL = 115;// use for check if the table is
	// completely created
	static MyDataBase_T_NUTRITION.DataBase myDataBaseHelper;
	private SQLiteDatabase db;// the order is name, type,latitude,longtitude,
	final static String TABLENAME = "T_NUTRITION";

	public MyDataBase_T_NUTRITION(Context context) {
		myDataBaseHelper = new MyDataBase_T_NUTRITION.DataBase(context);
		this.db = myDataBaseHelper.getWritableDatabase();
		//initMyDataBase(db);
	}

	public void createTable(String tableName) {
		String sql = "create table if not exists "
				+ tableName
				+ " (RecNo integer primary key autoincrement, _ID varchar(20) not null ,NAME varchar(20) not null, NAME_EN TEXT(60) not null,UNIT TEXT(60) not null,INFO TEXT(10) not null );";
		db.execSQL(sql);
	}

	// init database
	public void initMyDataBase(SQLiteDatabase db) {
		int placeTotal = 0;
		this.createTable(TABLENAME);
		if (this.SearchT_NUTRITION("*") != null)
			placeTotal = this.SearchT_NUTRITION("*").size();
		// if is completely create then return
		if (placeTotal == MyDataBase_T_NUTRITION.PLACETOTAL)
			return;
		// or insert
		//myDataBaseHelper.deleteTable();
		this.createTable(TABLENAME);

		this.Insert(1, 1, "���q", "Calory", "�d�d", "");

	}

	public void deleteTabel() {

	}

	// insert operating
	public void Insert(int RecNo, int _ID, String NAME, String NAME_EN,
			String UNIT, String INFO) {
		// the order is :name, type,longtitude,latitude

		this.db.execSQL(String
				.format("insert into %s (RecNo,_ID, NAME, NAME_EN ,UNIT,INFO) values('%s','%s','%s','%s','%s','%s')",
						TABLENAME, RecNo, _ID, NAME, NAME_EN, UNIT, INFO));
	}

	// update operation
	public void Update(String field, Object value, String where_expression) {
		this.db.execSQL(String.format("update %s set password=%s where %s ;",
				TABLENAME, field, value, where_expression));
	}

	// Delete operation
	public void Delete(String where_expression) {
		this.db.execSQL(String.format("delete from %s  where %s ;", TABLENAME,
				where_expression));
	}

	public ArrayList<MyDataBase_T_NUTRITION.T_NUTRITION> SearchT_NUTRITION(
			String selectWhat) {
		ArrayList<MyDataBase_T_NUTRITION.T_NUTRITION> FOODSList = new ArrayList<MyDataBase_T_NUTRITION.T_NUTRITION>();
		Cursor cursor = db.rawQuery("select " + selectWhat + " from "
				+ TABLENAME, null);
		while (cursor.moveToNext()) {
			int _ID = cursor.getInt(0);
			String NAME = cursor.getString(1);
			String NAME_EN = cursor.getString(2);
			String UNIT = cursor.getString(3);
			String NIFO = cursor.getString(4);

			// here create a Place class to add info.
			FOODSList
					.add(new T_NUTRITION(   _ID, NAME, NAME_EN, UNIT, NIFO));

		}
		cursor.close();

		return FOODSList;
	}

	public ArrayList<MyDataBase_T_NUTRITION.T_NUTRITION> SearchT_NUTRITION(
			String selectWhat, String where_expression) {
		ArrayList<MyDataBase_T_NUTRITION.T_NUTRITION> FOODSList = new ArrayList<MyDataBase_T_NUTRITION.T_NUTRITION>();
		Cursor cursor = db.rawQuery(String.format(
				"select %s from %s where %s;", selectWhat, TABLENAME,
				where_expression), null);
		while (cursor.moveToNext()) {

			int _ID = cursor.getInt(0);
			String NAME = cursor.getString(1);
			String NAME_EN = cursor.getString(2);
			String UNIT = cursor.getString(3);
			String INFO = cursor.getString(4);

			// here create a Place class to add info.

			FOODSList
					.add(new T_NUTRITION(   _ID, NAME, NAME_EN, UNIT, INFO));

		}
		cursor.close();
		return FOODSList;
	}

	public void Close() {
		this.db.close();
	}

	public class DataBase extends DatabaseHelper {
		public DataBase(Context context) {

			super(context );

			// TODO Auto-generated constructor stub
		}

	}

	// here is a Type of Place
	public class T_NUTRITION {

		public int _ID;
		public String NAME;
		public String NAME_EN;
		public String UNIT;
		public String INFO;

		T_NUTRITION(     int _ID, String NAME, String NAME_EN,
				String UNIT, String INFO) {
			// T_FOODS(int RecNo,String locale ) {
			this._ID = _ID;
			this.NAME = NAME;
			this.NAME_EN = NAME_EN;
			this.UNIT = UNIT;
			this.INFO = INFO;

		}

	}

}